using SimpleJSON;
using UnityEngine.UI;

namespace Gj
{
    public class ChatNewHelper : NodeHelper
    {
        public ChatItemHelper item;
        [SelectRef(UIKey.DATA_KEY)] public SelectKey channelKey;
        private ScrollRect _ScrollRect;

        protected override BindDataType bindDataType => BindDataType.All;

        private ScrollRect ScrollRect
        {
            get
            {
                if (_ScrollRect == null) _ScrollRect = GetComponent<ScrollRect>();
                return _ScrollRect;
            }
        }

        protected override void BindData(JSONObject json)
        {
            string channel = channelKey.Get(json);
            JSONNode data = dataKey.Get(json).AsObject;
            if (channel == null || data[channel] == null)
                BindData(new JSONArray());
            else
                BindData(data[channel].AsArray);
        }

        protected override void BindData(JSONArray arr)
        {
            if (arr.Count == 0)
            {
                item.gameObject.SetActive(false);
                return;
            }

            if (!item.gameObject.activeSelf) item.gameObject.SetActive(true);
            item.SetData(arr[arr.Count - 1].AsObject);
        }
    }
}